<?php

class Admin_DashboardController extends Zend_Controller_Action {

    private $userInfo;
    private $log;

    public function init() {
        $this->log = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
    }

    public function preDispatch() {
        // Autenticacion del usuario
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_helper->layout->disableLayout();
            $this->_forward("index", "index", "");
        } else {
            $identity = $auth->getIdentity();
            if ($identity['tipo_usuario'] != VirtualLabs_Admin_Usuario::ROL_ADMIN) {
                $this->_helper->layout->disableLayout();
                $this->_redirect('/');
            }
        }
        //fin de la autenicacion del usuario
    }

    public function indexAction() {
        // action body
    }

    public function tablerodataAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout->disableLayout();
        $reserved = new VirtualLabs_Reserved();
        try {
            $response = $reserved->getReservedResources();
            $response['response'] = 'success';
            echo Zend_Json_Encoder::encode($response);
        } catch (Exception $e) {
            echo Zend_Json_Encoder::encode(array("response" => "error", "msg" => "Ha ocurrido un error al comunicar con la plataforma."));
            $this->log->err($e->getTraceAsString());
        }
    }

    public function tablerounidaddataAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout->disableLayout();
        $reserved = new VirtualLabs_Reserved();
        try {
            $response = $reserved->getReservedResourcesVMUnits();
            $response['response'] = 'success';
            echo Zend_Json_Encoder::encode($response);
        } catch (Exception $e) {
            echo Zend_Json_Encoder::encode(array("response" => "error", "msg" => "Ha ocurrido un error al comunicar con la plataforma."));
            $this->log->err($e->getTraceAsString());
        }
    }

    public function tablerounidadAction() {
        $config = new Zend_Config_Ini(CLOUDSTACK_CONFIG_FILE, APPLICATION_ENV);
        $this->view->baseMemory = $config->get("base_memory");
        $this->view->baseCpuSpeed = $config->get("base_core_speed");
        $this->view->baseCpuNumber = $config->get("base_core_number");
//        $this->view->baseCpu = $config->get("base_core_speed");
    }

    public function tableroAction() {
        $hours = array('06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '22', '23', '00', '01', '02', '03', '04', '05');
        $table = "";
        foreach ($hours as $h) {
            $h1 = $h + 1;
            if (strlen($h1) == 1) {
                $h1 = "0$h1";
            }

            $table .= "<tr>
                <td>$h - $h1</td>
                <td><div id='$h-Do'></div></td>
                <td><div id='$h-Lu'></div></td>
                <td><div id='$h-Ma'></div></td>
                <td><div id='$h-Mi'></div></td>
                <td><div id='$h-Ju'></div></td>
                <td><div id='$h-Vi'></div></td>
                <td><div id='$h-Sa'></div></td>
                </tr>";
        }
        $this->view->body = $table;
    }

    public function cantidadlaboratoriosAction() {
        $lab = new VirtualLabs_Admin_Laboratorio();
        $this->view->cantidadLaboratorios = $lab->getLaboratorio(array('cuenta' => 'count(*)'), array());
    }

    public function cantidadmaquinasvirtualestotalAction() {
        $vm = new VirtualLabs_Admin_Machine();
        $this->view->cantidadVm = $vm->getVirtualMachine(array('cuenta' => 'count(*)'), array());
    }

    public function cantidadmaquinasvirtualesencendidasAction() {
        $vm = new VirtualLabs_Admin_Machine();
        $this->view->cantidadVmOn = $vm->getVirtualMachine(array('cuenta' => 'count(*)'), array('encendida' => 1));
    }

}

